package com.fhzn.auth.web.request;

import com.fhzn.auth.entity.DataResource;
import com.fhzn.auth.enums.CommonStatusEnum;
import com.fhzn.commons.toolkit.exception.BuzException;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * @author 老顾
 * @apiNote
 * @since 2024/1/19
 */
@Data
public class DataResourceOpRequest extends BaseRequest {

    private Integer id;

    private String name;

    private String code;

    private String parentCode;

    private Integer resourceType;

    private String comment;

    private Integer status;

    private Integer sort;


    public void checkAdd() {
        this.checkCommon();
    }

    public void checkChangeStatus() {
        if (id == null && (resourceType == null && code == null)) {
            throw new BuzException("id 或者 资源类型+资源Code不能为空");
        }
        if (status != 1 && status != 0) {
            throw new BuzException("要更新的资源状态不正确");
        }
        this.checkBaseParams();
    }

    public void checkModifyName() {
        if (id == null && (resourceType == null && code == null)) {
            throw new BuzException("id 或者 资源类型+资源Code不能为空");
        }
        if (StringUtils.isEmpty(name)) {
            throw new BuzException("资源名称不能为空");
        }
        this.checkBaseParams();
    }

    public void checkModify() {
        if (id == null && (resourceType == null && code == null)) {
            throw new BuzException("id 或者 资源类型+资源Code不能为空");
        }
        if (StringUtils.isEmpty(name) && StringUtils.isEmpty(parentCode) && Objects.isNull(sort)) {
            throw new BuzException("name,parentCode,sort 不能都为空");
        }
        this.checkBaseParams();
    }


    public void checkUpdate() {
        if (id == null || resourceType == null) {
            throw new BuzException("id或者资源类型不能为空");
        }
        this.checkCommon();
    }

    public void checkQuery() {
        if (getInvokeAppName() == null || resourceType == null) {
            throw new BuzException("appCode或者资源类型不能为空");
        }
        this.checkCommon();
    }

    public void checkParentQuery() {
        if (getInvokeAppName() == null || parentCode == null) {
            throw new BuzException("appCode或者父资源类型不能为空");
        }
        this.checkCommon();
    }

    public void checkCommon() {
        this.checkBaseParams();

        if (StringUtils.isBlank(getInvokeAppName())) {
            throw new BuzException("所属应用不能为空");
        }
        if (status == null || !CommonStatusEnum.contains(status)) {
            throw new BuzException("状态位不正确");
        }

    }

    /**
     * 生成发往服务端的query对象
     *
     * @return
     */
    @Override
    public DataResource genServiceParam() {
        DataResource dataResource = new DataResource();
        dataResource.setId(this.getId());
        dataResource.setResourceType(this.getResourceType());
        dataResource.setName(this.getName());
        dataResource.setCode(this.getCode());
        dataResource.setParentCode(this.getParentCode());
        dataResource.setStatus(this.getStatus());
        dataResource.setSort(this.getSort());
        dataResource.setComment(this.getComment());
        dataResource.setAppCode(this.getInvokeAppName());
        dataResource.setModifier(this.getHandler());
        return dataResource;
    }

    public DataResource addGenServiceParam() {
        DataResource resource = genServiceParam();
        resource.setStatus(1);
        resource.setCreator(this.getHandler());
        return resource;
    }

}
